home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: Please help me elect rounding of int division
- Date: 28 Feb 96 00:03:52 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.825465832@rscernix>
- References: <4grtbc$t9f@chaos.kulnet.kuleuven.ac.be>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4grtbc$t9f@chaos.kulnet.kuleuven.ac.be> Andreas.DeTroy@ped.kuleuven.ac.be (Andreas De Troy) writes:
-
- >How about this:
- >
- >#include <float.h>
- ^^^^^^^^^
- Make it <math.h>. Your code doesn't need the stuff from <float.h> and
- floor() is declared in <math.h>, not in <float.h>.
-
- >long rounddiv (long a, long b)
- >{
- > return ((long) (floor (((float) a / b) + 0.5)));
- ^^^^^^
- What's the point of this cast? The compiler knows that it has to return
- a long and it also knows how to obtain one from a double value, it doesn't
- hints from you :-)
-
- The only thing achieved by the usage of unnecessary casts and the
- excessive use of unneeded parentheses is a reduction in the readability
- and maintainability of the code.
-
- return floor((float) a / b + 0.5);
-
- looks much cleaner (and it saves a lot of typing :-)
-
- I didn't address the correctness of the rounding algorithm used, because
- the subject has been already beaten to death in this newsgroup.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-